<template>
  <a-modal v-model:open="isShow" title="编辑" @cancel="handleCancel" style="width: 800px">
    <a-form
        :model="configState"
        :label-col="{span: 4}"
        :wrapper-col="{ span: 18 }"
    >
      <a-row>
        <a-col :span="24">
          <a-form-item ref="warehouseCode" label="编码" name="warehouseCode">
            <a-input  disabled v-model:value="configState.warehouseCode" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="warehouseName" label="名称" name="warehouseName">
            <a-input  disabled v-model:value="configState.warehouseName" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="warehouseStatus" label="状态" name="warehouseStatus">
            <a-input  disabled v-model:value="configState.warehouseStatus" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="warehouseType" label="类型" name="warehouseType">
            <a-select  v-model:value="configState.warehouseType" :options="selectList.type"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="site" label="站点" name="site">
            <a-select  v-model:value="configState.site" :options="selectList.site"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="saleZone" label="销售区域" name="saleZone">
            <a-select  v-model:value="configState.saleZone" :options="selectList.sale_zone"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="warehouseNameEn" label="仓库英文名称" name="warehouseNameEn">
            <a-input  v-model:value="configState.warehouseNameEn" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="warehouseCountry" label="国家" name="warehouseCountry">
            <a-select  v-model:value="configState.warehouseCountry" :options="selectList.country"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="zipCode" label="邮编" name="zipCode">
            <a-input  disabled v-model:value="configState.zipCode" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="stateCode" label="州编码" name="stateCode">
            <a-input  disabled v-model:value="configState.stateCode" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="cityName" label="城市名" name="cityName">
            <a-input  disabled v-model:value="configState.cityName" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="addressLine1" label="地址1" name="addressLine1">
            <a-input  disabled v-model:value="configState.addressLine1" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="addressLine2" label="地址2" name="addressLine2">
            <a-input  disabled v-model:value="configState.addressLine2" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="contactPerson" label="联系人" name="contactPerson">
            <a-input  disabled v-model:value="configState.contactPerson" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="contactPhone" label="联系电话" name="contactPhone">
            <a-input  disabled v-model:value="configState.contactPhone" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="contactEmail" label="联系邮箱" name="contactEmail">
            <a-input  v-model:value="configState.contactEmail" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="warehouseService" label="服务商" name="warehouseService">
            <a-input  disabled v-model:value="configState.warehouseService" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="serviceCode" label="服务商仓库代码" name="serviceCode">
            <a-input  v-model:value="configState.serviceCode" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="memo" label="备注" name="memo">
            <a-textarea  v-model:value="configState.memo" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <template #footer>
      <a-button @click="handleCancel">取消</a-button>
      <a-button type="primary" :loading="btnLoading" @click="handleOk">确定</a-button>
    </template>
  </a-modal>
</template>
<script setup>
import { ref, computed,watch,reactive,onMounted } from 'vue'
import {message} from "ant-design-vue";
import {getManyDictItems} from "@/api/system/dict.js";
import {editWarehouse, getWarehouseDetail} from "@/api/basicInformation/warehouseConfiguration.js";
const props = defineProps({
  openEdit:{
    type: Boolean,
    default: false
  },
  warehouseCode:{
    type: String,
    default: ''
  }
})
const selectList = reactive({
  type:[],
  site:[],
  sale_zone:[],
  country:[]
})
const configState = ref({
  "warehouseCode": "",
  "warehouseName": "",
  "warehouseStatus": "",
  "warehouseType": "",
  "updateTime": "",
  "warehouseNameEn": "",
  "zipCode": "",
  "stateCode": "",
  "cityName": "",
  "addressLine1": "",
  "addressLine2": "",
  "addressType": "",
  "contactPerson": "",
  "contactPhone": "",
  "contactEmail": "",
  "memo": "",
  "warehouseCountry": "",
  "site": "",
  "saleZone": "",
  "warehouseService": "",
  "serviceCode": ""
})
const emit = defineEmits(['close','update'])
const isShow = computed(()=>{
  return props.openEdit
})
const btnLoading = ref(false)
const handleCancel = () => {
  emit('close')
}
const handleOk = async () => {
  try{
    btnLoading.value = true
    const res = await editWarehouse(configState.value)
    message.success('修改成功')
    emit('close')
    emit('update')
  }catch (e){
    message.error(e)
  }finally {
    btnLoading.value = false
  }
}
/**
 * 初始化字典数据
 * @returns {Promise<void>}
 */
const init = async () => {
  try{
    const res = await getManyDictItems({
      dictCodeList:"warehouse_data_type,country,site,sale_zone"
    })
    selectList.site = res.result.site
    selectList.type = res.result.warehouse_data_type
    selectList.country = res.result.country
    selectList.sale_zone = res.result.sale_zone
  }catch (e){
    console.log(e)
  }
}
onMounted(() => {
  init()
})
watch(()=>isShow.value, async (val) => {
  if(val){
    const res = await getWarehouseDetail({warehouseCode: props.warehouseCode})
    configState.value = res.result
  }
})
</script>
<style scoped>

</style>
